package com.xhu.controller;

import com.xhu.entity.User;
import com.xhu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;

    @PostMapping(value = "/getUser")
    public String GetUser(Integer userId){
        log.debug(userId.toString());
        return userService.getUserInfo(userId).toString();
    }

    @PostMapping(value = "/getUserInfo")
    public User GetUserInfo(){
        User userInfo = (User) session.getAttribute("userInfo");
        log.debug("后面的sessionId: "+session.getId());
        log.debug(userInfo.toString());
        return userInfo;
    }

    @PostMapping(value = "/register")
    public String register(@RequestBody User user){
        log.debug(user.toString());
        String message = "true";
        //查看注册用户名是否存在
        if(userService.checkUserAccount(user.getUserAccount())){
            return "用户名存在！";
        }else{
            //用户名不存在则添加用户信息
            boolean re = userService.register(user);
            if(!re){
                message = "未知错误账号申请失败！！";
            }
        }

        return message;
    }
}
